package com.lisa.passjava.question.controller;

import java.util.Arrays;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lisa.passjava.question.entity.QuestionEntity;
import com.lisa.passjava.question.service.QuestionService;
import com.lisa.common.utils.PageUtils;
import com.lisa.common.utils.R;

import javax.validation.Valid;


/**
 * 
 *http://localhost:8060/api/question/v1/admin/question/list?t=1640828344740&page=1&limit=10&key=
 * @author lisa
 * @email 1731695504@qq.com
 * @date 2021-12-24 14:01:32
 */
@RestController
@RequestMapping("question/v1/admin/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;



    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = questionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("question:question:info")
    public R info(@PathVariable("id") Long id){
		QuestionEntity question = questionService.getById(id);

        return R.ok().put("question", question);
    }

    /**
     * 两种方式：
     * 使用Mysql保存
     * 使用ES保存
     */
    /*@RequestMapping("/create")
    //@RequiresPermissions("question:question:save")
    public R create(@Valid @RequestBody QuestionEntity question){
		questionService.save(question);
		//throw new Exception("test");

        return R.ok();
    }*/

    /**
     * 使用ES保存
     * @param question
     * @return
     */
    //@PostMapping("/save")
    @PostMapping("/create")
    public R save(@RequestBody QuestionEntity question){
        questionService.saveQuestion(question);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("question:question:update")
    public R update(@RequestBody QuestionEntity question){
		questionService.updateById(question);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("question:question:delete")
    public R delete(@RequestBody Long[] ids){
		questionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
